﻿using CNKI.TPI.Web.Admin.IDAO;
using CNKI.TPI.Web.Admin.Model;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
namespace CNKI.TPI.Web.Admin.DAO
{
    public class UserMgmtDAO : BaseDAO, IUserMgmtDAO
    {
        /// <summary>
        /// 获取所有用户组列表
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_GROUP> GetGroup()
        {
            IList<SYS_CMS_GROUP> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_GROUP>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取所有用户组关系表
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_USER_GROUP> GetUserGroup()
        {
            IList<SYS_CMS_USER_GROUP> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_USER_GROUP>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取所有用户IP
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_USER_IP> GetGroupIpByUserID(int userid)
        {
            IList<SYS_CMS_USER_IP> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_USER_IP> exp = new ExpressionCombine<SYS_CMS_USER_IP>();
                exp.And("UrID", OperatorType.Equal, userid);
                result = Context.Get<SYS_CMS_USER_IP>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 保存IP段
        /// </summary>
        /// <returns></returns>
        public bool InsertUserIp(SYS_CMS_USER_IP obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_USER_IP>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除对应的用户组
        /// </summary>
        /// <returns></returns>
        public bool DeleteUserGroup(string sysid)
        {
            bool result = false;
            sysid = sysid.Replace(',', '+');
            try
            {
                string sql = "delete FROM SYS_CMS_GROUP where sysid =(" + sysid + ")";
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除对应的ip段
        /// </summary>
        /// <returns></returns>
        public bool DeleteUserIP(string sysidstr)
        {
            bool result = false;
            sysidstr = sysidstr.Replace(',', '+');
            try
            {
                string sql = "delete FROM SYS_CMS_USER_IP where sysid =(" + sysidstr + ")";
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 更新IP段
        /// </summary>
        /// <returns></returns>
        public bool UpdateUserIp(SYS_CMS_USER_IP obj)
        {
            bool result = false;
            try
            {
                result = Context.Update<SYS_CMS_USER_IP>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取单位名称
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_USER_UNIT> GetUnit()
        {
            IList<SYS_CMS_USER_UNIT> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_USER_UNIT>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取部门
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_USER_UNIT_DEPT> GetDeptByUnitID(int uid)
        {
            IList<SYS_CMS_USER_UNIT_DEPT> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_USER_UNIT_DEPT> exp = new ExpressionCombine<SYS_CMS_USER_UNIT_DEPT>();
                exp.And("UNITID", OperatorType.Equal, uid);
                result = Context.Get<SYS_CMS_USER_UNIT_DEPT>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取所有用户列表
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_USER> GetUserList()
        {
            IList<SYS_CMS_USER> result = null;
            try
            {
                result = Context.GetAll<SYS_CMS_USER>();
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 保存用户信息
        /// </summary>
        /// <returns></returns>
        public bool InsertUser(SYS_CMS_USER obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_USER>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <returns></returns>
        public bool UpdateUser(SYS_CMS_USER obj)
        {
            bool result = false;
            try
            {
                result = Context.Update<SYS_CMS_USER>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 保存用户组关联表信息
        /// </summary>
        /// <returns></returns>
        public bool InsertUserGroup(SYS_CMS_USER_GROUP obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_USER_GROUP>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 根据用户ID删除对应的用户组关联信息
        /// </summary>
        /// <returns></returns>
        public bool DelUserGroupByUserID(int uid)
        {
            bool result = false;
            try
            {
                ExpressionCombine<SYS_CMS_USER_GROUP> exp = new ExpressionCombine<SYS_CMS_USER_GROUP>();
                exp.And("UserID", OperatorType.Equal, uid);
                result = Context.Delete<SYS_CMS_USER_GROUP>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 根据用户ID删除对应的用户IP关联信息
        /// </summary>
        /// <returns></returns>
        public bool DelUserIPByUserID(int uid)
        {
            bool result = false;
            try
            {
                ExpressionCombine<SYS_CMS_USER_IP> exp = new ExpressionCombine<SYS_CMS_USER_IP>();
                exp.And("URID", OperatorType.Equal, uid);
                exp.And("Flag", OperatorType.Equal, "0");
                result = Context.Delete<SYS_CMS_USER_IP>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 根据用户ID删除对应的用户IP关联信息
        /// </summary>
        /// <returns></returns>
        public bool DelGroupIPByGroupID(int gid)
        {
            bool result = false;
            try
            {
                ExpressionCombine<SYS_CMS_USER_IP> exp = new ExpressionCombine<SYS_CMS_USER_IP>();
                exp.And("URID", OperatorType.Equal, gid);
                exp.And("Flag", OperatorType.Equal, "1");
                result = Context.Delete<SYS_CMS_USER_IP>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取所有数据库列表 分页
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_USER> GetUserPageSize(int start, int length, out int count)
        {

            IList<SYS_CMS_USER> result = null;
            count = 0;
            try
            {
                result = Context.Gets<SYS_CMS_USER>(null, start, length, out count);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }
        /// <summary>
        /// 获取所有用户列表 分页
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_USER> GetUserByNamePageSize(string username, int start, int length, out int count)
        {
            count = 0;
            IList<SYS_CMS_USER> result = null;
            try
            {
            ExpressionCombine<SYS_CMS_USER> exp = new ExpressionCombine<SYS_CMS_USER>();
            Expression<Func<SYS_CMS_USER, bool>> match = null; //查询条件
            if (!String.IsNullOrEmpty(username))
            {
                exp.And("UserName", OperatorType.Equal, username);
                match = exp.Compile();
            }
            result = Context.Gets<SYS_CMS_USER>(match, start, length, out count);
               }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 根据用户组名称，查找用户组
        /// </summary>
        /// <param name="groupname"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<SYS_CMS_GROUP> GetGroupByNamePageSize(string groupname, int start, int length, out int count)
        {
            count = 0;
            IList<SYS_CMS_GROUP> result = null;
            try
            {
                ExpressionCombine<SYS_CMS_GROUP> exp = new ExpressionCombine<SYS_CMS_GROUP>();
                Expression<Func<SYS_CMS_GROUP, bool>> match = null; //查询条件
                if (!String.IsNullOrEmpty(groupname))
                {
                    exp.And("Name", OperatorType.Equal, groupname);
                    match = exp.Compile();
                }
                result = Context.Gets<SYS_CMS_GROUP>(match, start, length, out count);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        ///根据用户组 获取所有用户列表 分页
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_USER> GetUserByGroupPageSize(int groupID, int start, int length, out int count)
        {
            IList<SYS_CMS_USER> result = null;
            count = 0;
            try
            {
                IList<SYS_CMS_USER_GROUP> listgroup = GetUserGroup();
                IEnumerable<int> useridstr = listgroup.Where(c => c.GroupID == groupID).Select(c => c.UserID).Distinct();
                //string str = string.Join("+",useridstr);

                if (useridstr.Sum() != 0) //该用户组对应的有用户则查询用户 
                {
                    ExpressionCombine<SYS_CMS_USER> exp = new ExpressionCombine<SYS_CMS_USER>();
                    Expression<Func<SYS_CMS_USER, bool>> match = null; //查询条件
                    foreach (int item in useridstr)
                    {
                        exp.Or("SysID", OperatorType.Equal, item);
                    }
                    match = exp.Compile();
                    result = Context.Gets<SYS_CMS_USER>(match, start, length, out count);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除对应的用户
        /// </summary>
        /// <returns></returns>
        public bool DeleteUser(string sysidstr)
        {
            bool result = false;
            sysidstr = sysidstr.Replace(',', '+');
            try
            {
                string sql = "delete FROM SYS_CMS_USER where sysid =(" + sysidstr + ")";
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 新增用户组
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool InsertGroup(SYS_CMS_GROUP obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_GROUP>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 更新用户组
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool UpdateGroup(SYS_CMS_GROUP obj)
        {
            bool result = false;
            try
            {
                result = Context.Update<SYS_CMS_GROUP>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 修改时判断组名是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistUserGroupUpdate(string name, int sysid)
        {
            bool result = false;
            try
            {
                IList<SYS_CMS_GROUP> obj = null;
                string lowerstr = name.ToLower();
                string upperstr = name.ToUpper();
                string sql = "SELECT * FROM SYS_CMS_GROUP where name='{0}' or name='{1}'";
                obj = Context.ExecuteSql<SYS_CMS_GROUP>(string.Format(sql, lowerstr,upperstr));
                if (obj != null && obj.Count > 0 && obj[0].SysID != sysid)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 删除密级
        /// </summary>
        /// <param name="sysidstr"></param>
        /// <returns></returns>
        public bool DeleteSecurity(string sysidstr)
        {
            bool result = false;
            sysidstr = sysidstr.Replace(',', '+');
            try
            {
                string sql = "delete FROM SYS_CMS_SECURITY where sysid =(" + sysidstr + ")";
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 修改时判断密级名称是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistSecurityName(string name, int sysid)
        {
            bool result = false;
            try
            {
                IList<SYS_CMS_SECURITY> obj = null;
                string sql = "SELECT * FROM SYS_CMS_SECURITY where name='{0}'";
                obj = Context.ExecuteSql<SYS_CMS_SECURITY>(string.Format(sql, name));
                if (obj != null && obj.Count > 0 && obj[0].SysID != sysid)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 修改时判断密级值是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistSecurityValue(string value, int sysid)
        {
            bool result = false;
            try
            {
                IList<SYS_CMS_SECURITY> obj = null;
                string sql = "SELECT * FROM SYS_CMS_SECURITY where value='{0}'";
                obj = Context.ExecuteSql<SYS_CMS_SECURITY>(string.Format(sql, value));
                if (obj != null && obj.Count > 0 && obj[0].SysID != sysid)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 保存密级信息
        /// </summary>
        /// <returns></returns>
        public bool InsertSecurity(SYS_CMS_SECURITY obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_SECURITY>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 更新密级信息
        /// </summary>
        /// <returns></returns>
        public bool UpdateSecurity(SYS_CMS_SECURITY obj)
        {
            bool result = false;
            try
            {
                result = Context.Update<SYS_CMS_SECURITY>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 获取用户单位 分页
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_UNIT> GetUnitPageSize(string unitname, int start, int length, out int count)
        {
            IList<SYS_CMS_USER_UNIT> result = null;
            count = 0;
            try
            {
                ExpressionCombine<SYS_CMS_USER_UNIT> exp = new ExpressionCombine<SYS_CMS_USER_UNIT>();
                Expression<Func<SYS_CMS_USER_UNIT, bool>> match = null; //查询条件
                if (!String.IsNullOrEmpty(unitname))
                {
                    exp.And("unitname", OperatorType.Equal, unitname);
                    match = exp.Compile();
                }
                result = Context.Gets<SYS_CMS_USER_UNIT>(match, start, length, out count);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 保存用户单位信息
        /// </summary>
        /// <returns></returns>
        public bool InsertUnit(SYS_CMS_USER_UNIT obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_USER_UNIT>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 更新用户单位信息
        /// </summary>
        /// <returns></returns>
        public bool UpdateUnit(SYS_CMS_USER_UNIT obj)
        {
            bool result = false;
            try
            {
                result = Context.Update<SYS_CMS_USER_UNIT>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取下一个用户单位编码
        /// </summary>
        /// <returns></returns>
        public string GetNextUnitCode()
        {
            string strNextUnitCode = "0001";
            string sql = "SELECT unitcode FROM SYS_CMS_USER_UNIT order by unitcode desc";
            IList<SYS_CMS_USER_UNIT> obj = null;
            obj = Context.ExecuteSql<SYS_CMS_USER_UNIT>(sql);
            if (obj != null && obj.Count > 0 && obj[0].UNITCODE != "")
            {
                string thiscode = obj[0].UNITCODE;
                strNextUnitCode = (Convert.ToInt32(thiscode) + 1).ToString();
                int nCodeLength = strNextUnitCode.Length;
                for (int i = 0; i < 4 - nCodeLength; i++)
                {
                    strNextUnitCode = "0" + strNextUnitCode;
                }
            }
            return strNextUnitCode;

        }
        /// <summary>
        /// 删除用户单位
        /// </summary>
        /// <param name="sysidstr"></param>
        /// <returns></returns>
        public bool DeleteUnit(string sysidstr)
        {
            bool result = false;
            sysidstr = sysidstr.Replace(',', '+');
            try
            {
                string sql = "delete FROM SYS_CMS_USER_UNIT where sysid =(" + sysidstr + ")";
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 判断单位名称 是否存在（修改）
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistUnitUpdate(string name, int sysid)
        {
            bool result = false;
            try
            {
                IList<SYS_CMS_USER_UNIT> obj = null;
                string sql = "SELECT * FROM SYS_CMS_USER_UNIT where unitname='{0}'";
                obj = Context.ExecuteSql<SYS_CMS_USER_UNIT>(string.Format(sql, name));
                if (obj != null && obj.Count > 0 && obj[0].SYSID != sysid)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取单位下的部门节点
        /// </summary>
        /// <param name="unitid">单位id</param>
        /// <param name="garde">节点级别</param>
        /// <param name="code">父节点code  当code为空时，即获取根节点</param>
        /// <returns></returns>
        public IList<SYS_CMS_USER_UNIT_DEPT> GetDept(int unitid, int garde, string code)
        {
            IList<SYS_CMS_USER_UNIT_DEPT> obj = null;
            try
            {
                string sql = "SELECT * FROM SYS_CMS_USER_UNIT_DEPT WHERE unitid= " + unitid + " and grade=" + garde;
                if (code != "")
                {
                    sql += " and syscode=" + code + "?";
                }

                obj = Context.ExecuteSql<SYS_CMS_USER_UNIT_DEPT>(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return obj;
        }
        public bool DeleteDept(string sysid, string syscode, int unitid)
        {
            bool result = false;
            try
            {
                string sql = "delete FROM SYS_CMS_USER_UNIT_DEPT where sysid =" + sysid;
                string sqlchild = "delete FROM SYS_CMS_USER_UNIT_DEPT where unitid=" + unitid + " and syscode =" + syscode + "?";//删除该节点下的所有节点
                if (Context.ExecuteNonQuery(sql))
                {
                    if (Context.ExecuteNonQuery(sqlchild))
                    {
                        //如果该节点的父节点已经没有子节点，则更新HASCHILDFLAG标识
                        string parentcode = syscode.Substring(0, syscode.Length - 4);
                        string sqlparent = "select * FROM SYS_CMS_USER_UNIT_DEPT where unitid=" + unitid + " and syscode =" + parentcode + "?";

                        IList<SYS_CMS_USER_UNIT_DEPT> obj = null;
                        obj = Context.ExecuteSql<SYS_CMS_USER_UNIT_DEPT>(sqlparent);
                        if (obj != null && obj.Count == 1)//没有子节点
                        {
                            UpdateDeptChild(parentcode, unitid, 0);
                        }
                    }
                }
                result = true;
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 插入部门信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool InsertDept(SYS_CMS_USER_UNIT_DEPT obj)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_USER_UNIT_DEPT>(obj);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 更新部门信息
        /// </summary>
        /// <returns></returns>
        public bool UpdateDeptName(int id, string deptname)
        {
            bool result = false;
            try
            {
                string sql = "update SYS_CMS_USER_UNIT_DEPT set deptname='" + deptname + "'  where sysid=" + id;
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 修改时判断部门名称是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool IsExistDeptUpdate(string name, int sysid, int unitid)
        {
            bool result = false;
            try
            {
                IList<SYS_CMS_USER_UNIT> obj = null;
                string sql = "SELECT * FROM SYS_CMS_USER_UNIT_DEPT where deptname='{0}' and unitid=" + unitid;
                obj = Context.ExecuteSql<SYS_CMS_USER_UNIT>(string.Format(sql, name));
                if (obj != null && obj.Count > 0 && obj[0].SYSID != sysid)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 获取下一个部门编码
        /// </summary>
        /// <param name="unitid"></param>
        /// <param name="parentcode"></param>
        /// <param name="grade"></param>
        /// <returns></returns>
        public string GetNextDeptCode(int unitid, string parentcode, int grade)
        {
            string deptcode = "0001";
            string sql = "SELECT * FROM SYS_CMS_USER_UNIT_DEPT where unitid= " + unitid + " and syscode=" + parentcode + "? and grade=" + grade + " order by syscode desc";
            IList<SYS_CMS_USER_UNIT_DEPT> obj = null;
            obj = Context.ExecuteSql<SYS_CMS_USER_UNIT_DEPT>(sql);

            if (obj != null && obj.Count > 0)
            {
                string strTempSysCode = obj[0].DEPTCODE;
                deptcode = (Convert.ToInt32(strTempSysCode) + 1).ToString();
                int nCodeLength = deptcode.Length;
                for (int i = 0; i < 4 - nCodeLength; i++)
                {
                    deptcode = "0" + deptcode;
                }
            }
            return deptcode;
        }
        /// <summary>
        /// 更新是否有子节点字段
        /// </summary>
        /// <param name="parentcode"></param>
        /// <param name="grade"></param>
        /// <returns></returns>
        public bool UpdateDeptChild(string parentcode, int unitid, int flage)
        {
            bool result = false;
            try
            {
                string sql = "update SYS_CMS_USER_UNIT_DEPT set HASCHILDFLAG=" + flage + "    where unitid=" + unitid + " and  syscode=" + parentcode;
                result = Context.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public bool UpdateDBRight(SYS_CMS_USER_ACL_DB_RIGHT dbRight)
        {
            bool result = false;
            try
            {
                result = Context.Update<SYS_CMS_USER_ACL_DB_RIGHT>(dbRight);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public bool InsertDBRight(SYS_CMS_USER_ACL_DB_RIGHT dbRight)
        {
            bool result = false;
            try
            {
                result = Context.Insert<SYS_CMS_USER_ACL_DB_RIGHT>(dbRight);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 根据用户ID删除对应的用户组关联信息
        /// </summary>
        /// <returns></returns>
        public bool DelUserRight(int uid, int dbid, int rightType)
        {
            bool result = false;
            try
            {
                ExpressionCombine<SYS_CMS_USER_ACL_DB_RIGHT> exp = new ExpressionCombine<SYS_CMS_USER_ACL_DB_RIGHT>();
                exp.And("URID", OperatorType.Equal, uid);
                exp.And("DBID", OperatorType.Equal, dbid);
                exp.And("Flag", OperatorType.Equal, rightType);
                result = Context.Delete<SYS_CMS_USER_ACL_DB_RIGHT>(exp.Compile());
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        public SYS_CMS_USER GetUserInfoByID(int userid)
        {
            SYS_CMS_USER obj = new SYS_CMS_USER();
            try
            {
                ExpressionCombine<SYS_CMS_USER> exp = new ExpressionCombine<SYS_CMS_USER>();
                exp.And("SysID", OperatorType.Equal, userid);
                IList<SYS_CMS_USER> list = Context.Get<SYS_CMS_USER>(exp.Compile());
                if (list != null && list.Count > 0)
                {
                    obj = list[0];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return obj;
        }
    }
}
